package com.lframework.starter.web.core.listeners;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.lframework.starter.web.core.event.ClearTenantEvent;
import com.lframework.starter.web.core.event.ReloadTenantEvent;
import com.lframework.starter.web.core.event.SetTenantEvent;
import com.lframework.starter.web.core.utils.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;

import javax.sql.DataSource;

public class TenantListener {

    public static class ReloadTenantListener implements ApplicationListener<ReloadTenantEvent>,
            Ordered {

        @Autowired
        private DynamicDataSourceProperties dynamicDataSourceProperties;

        @Autowired
        private DataSource dataSource;

        @Override
        public void onApplicationEvent(ReloadTenantEvent event) {
            DataSourceProperty dataSourceProperty = dynamicDataSourceProperties.getDatasource()
                    .get("master");
            DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
            ds.addDataSource(String.valueOf(event.getTenantId()),
                    DataSourceUtil.createDataSource(dataSourceProperty, event.getJdbcUrl(),
                            event.getJdbcUsername(), event.getJdbcPassword(), event.getDriver()));
        }

        @Override
        public int getOrder() {
            return Integer.MIN_VALUE;
        }
    }

    public static class SetTenantListener implements ApplicationListener<SetTenantEvent> {

        @Override
        public void onApplicationEvent(SetTenantEvent event) {
            DynamicDataSourceContextHolder.push(String.valueOf(event.getTenantId()));
        }
    }

    public static class ClearTenantListener implements ApplicationListener<ClearTenantEvent> {

        @Override
        public void onApplicationEvent(ClearTenantEvent event) {
            DynamicDataSourceContextHolder.clear();
        }
    }
}
